<?PHP
require_once WACT_ROOT.'controller/controller.inc.php';
require_once WACT_ROOT.'controller/form.inc.php';
require_once WACT_ROOT. 'util/dataspace.inc.php';
require_once APP_ROOT.'view/common/layout.formview.php';

require_once APP_ROOT.'model/common/user.php';
require_once APP_ROOT.'model/common/categories.php';
require_once APP_ROOT.'model/common/session.php';
require_once APP_ROOT.'model/common/team.php';
require_once 'model/common/date.php';
require_once APP_ROOT . "model/common/site.php"; 


class TeamsIndexView extends LayoutFormView {
	function TeamsIndexView ($TemplateFile){
		parent::LayoutView($TemplateFile);

	}

	function prepare(&$controller, &$request, &$responseModel){
		parent::prepare();

		if(APP_NAME!=""){
			$this->Template->set('app_name',APP_NAME);
		} else $this->Template->set('app_name','.');

		if(!Session::isLogin())
			$this->Template->set('login',' ');
		$docbase="http://".$_SERVER['HTTP_HOST'];
		$this->Template->set('docbase',$docbase);

        $city = Site::getCity();
 
        
		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - Team profiles index');


		if(!Session::isLogin())
			$this->Template->set('login',' ');
		//load filters
		Session::loadResponseModel($responseModel,'filter.teams');

		//teams list
		$pager=& $this->Template->getChild('pagenav');
		if($responseModel->get('item'))
			$pager->Items=$responseModel->get('item');
		$list=&Team::getTeamsList($pager,$request,$responseModel,true);
		$this->Template->setChildDataSource('teams',$list);


		//populate categories
        $categories1 =& CategoriesModel::getSportCategories(false,false,true);
        $categories2 =& CategoriesModel::getCommunityCategories(false,false,true);
        $category1 =& $this->Template->getChild('category1');
        $category2 =& $this->Template->getChild('category2');
        $category1->setChoices($categories1);
        $valcategory1=$responseModel->get('category1');
        $category1->setSelection($valcategory1);
        $category2->setChoices($categories2);
        $valcategory2=$responseModel->get('category2');
        $category2->setSelection($valcategory2);

        $genders=array('(ALL)','boys','girls');
        $gender_el=&$this->Template->getChild('gender');
        $gender_el->setChoices($genders);
        $gender=$responseModel->get('gender')+1;
        $gender_el->setSelection($gender);

        $leagues=&Team::getLeaguesList(true);
        $league_el=&$this->Template->getChild('league_id');
        $league_el->setChoices($leagues);
        $league=$responseModel->get('league_id')+1;
        $league_el->setSelection($league);


		//item per page
		$item=&$this->Template->getChild('item');
		$item_list=array("10"=>"10","20"=>"20","25"=>"25","50"=>"50","100"=>"100");
		$item->setChoices($item_list);


	}
}

class TeamsIndexPage extends PageController {
	function TeamsIndexPage(){
		parent::PageController();

		$Form=new FormController();
		$Form->addChild('list',new ButtonController(new Delegate($this, 'teamslist')));
		$Form->setDefaultChild('list');
		$this->addChild('FilterForm',$Form);
		$this->setDefaultChild('FilterForm');

        
        $mytemplate = 'pub/teams/index.html';
     
		$this->setDefaultView(new TeamsIndexView($mytemplate));


	}

	function teamslist(&$controller,&$request,&$responseModel){

		if($responseModel->get('category1')) $responseModel->set('filter_category1',true);

		if($responseModel->get('category2')) $responseModel->set('filter_category2',true);


		if($responseModel->get('gender')!=0) {

			$responseModel->set('filter_gender',true);
		}
		$gender=$responseModel->get('gender')-1;
		$responseModel->set('gender',$gender);



		if($responseModel->get('league_id')!=0) {

			$responseModel->set('filter_league_id',true);

		}
		$league_id=$responseModel->get('league_id')-1;
		$responseModel->set('league_id',$league_id);

		Session::saveResponseModel($responseModel,'filter.teams');



	}
}
?>
